package common;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import math.probability.ExpDistribution;
import math.probability.LNormalDistribution;
import math.probability.LaplasDistribution;
import math.probability.NormalDistribution;
import math.probability.ProbDistribution;
import math.probability.UniformDistribution;

public class ErrorsInVariablesTestMain {
	
	
	public void doWork() throws FileNotFoundException {
		ProbDistribution distX = new ExpDistribution(1/Math.sqrt(2.0));
//		ProbDistribution distX = new LaplasDistribution();
//		ProbDistribution distX = new LNormalDistribution(-Math.log(3.0)/2.0, Math.log(3.0));
		ProbDistribution distEps = new NormalDistribution(0.0,0.5);
//		ProbDistribution distEps = new LaplasDistribution(4.5, 0.0);
	//	int nSet[] = {50, 100, 200, 300, 500, 1000};
		int nSet[] = {50};
		
//		int nSet[] = {300};
		Double b0 = 0.5;
		Double b1 = 1.0;
//		double s2Ksi[] = {0.5d, 1d, 1.5d};
		double s2Ksi[] = {0.5d};
		PrintWriter out = new PrintWriter(new FileOutputStream("BSTestLSMvsWLSM.csv", true));
		out.println("X,s2Ksi,N,alpha,beta");
		
		StatTest test = new BSTestLSMvsWLSM();
//		StatTest test = new X2Test();
//		StatTest test = new CorrTest();
//		StatTest test = new SpCorrTest();
//		StatTest test = new X2onLAMvsWLAM();
//		StatTest test = new X2onLAM();
		for (int n : nSet) {
		System.out.println("Calculating n = " + n);
		StructuralModel modelH0 = new StructuralModel(n, distX, new NormalDistribution(0.0,0.0), distEps, b0,b1);
		for (double s : s2Ksi) {
			
			StructuralModel modelH1 = new StructuralModel(n, distX, new NormalDistribution(0.0,s), distEps, b0,b1);
			Double M = 1000.0;
			StatTestTester tester = new StatTestTester(M.intValue(), test, modelH0, modelH1);
			tester.testAlpha_Beta();
			out.println(distX.toString() + "," + s + "," + n + "," + tester.getAlpha() + "," + tester.getBeta());
//			PrintWriter out1 = new PrintWriter(new FileOutputStream("statCorr"+distX.toString()+"_"+s+"_"+n+".csv"));
//			out1.println("H0_"+n+",H1_"+n);
//			ArrayList<Double> statH0 = tester.getStatH0();
//			ArrayList<Double> statH1 = tester.getStatH1();
//			for (int i = 0; i < statH0.size(); i++) {
//				out1.println(statH0.get(i) + "," + statH1.get(i));
//			}
//			out1.flush();
//			out1.close();
//			
		}
		}
		out.flush();
		out.close();
			
	}
	
	public void doWork1() throws FileNotFoundException {
//		ProbDistribution distX = new ExpDistribution(0.75);
		ProbDistribution distX = new UniformDistribution(1.0, 5.6);
		ProbDistribution distEps = new NormalDistribution(0.0,0.1);
//		ProbDistribution distEps = new LaplasDistribution(4.5, 0.0);
		Double b0 = 0.5;
		Double b1 = 1.0;
		double s2Ksi[] = {0.5d, 1d, 1.5d};
		PrintWriter out = new PrintWriter(new FileOutputStream("TestEstH1_2.csv", true));
		out.println("X,eps,b1,s2Ksi,mean(b),var(b)");
		StatTest test = new X2Test();
		StructuralModel modelH0 = new StructuralModel(50, distX, new NormalDistribution(0.0,0.0), distEps, b0,b1);
		for (double s : s2Ksi) {
			StructuralModel modelH1 = new StructuralModel(50, distX, new NormalDistribution(0.0,s), distEps, b0,b1);
			StatTestTester tester = new StatTestTester(500, test, modelH0, modelH1);
			tester.testEstVarH1();
			out.println(distX.toString() + "," + distEps.toString() + "," + b1 + "," + s + "," + 
					tester.getMeanDiff() + "," + tester.getVarDiff());
					
		}
		out.flush();
		out.close();
			
	}
	
	public void doWork2() throws FileNotFoundException {
		ProbDistribution distX = new ExpDistribution(0.75);
//		ProbDistribution distX = new UniformDistribution(1.0, 5.6);
		ProbDistribution distEps = new NormalDistribution(0.0,0.1);
//		ProbDistribution distEps = new LaplasDistribution(4.5, 0.0);
		Double b0 = 0.5;
		Double b1 = 1.0;
		double s2Ksi[] = {0.5d};
//		PrintWriter out = new PrintWriter(new FileOutputStream("TestEstH1.csv", true));
//		out.println("X,eps,b1,s2Ksi,mean(b-bw),var(b-bw)");
		StatTest test = new X2onLAM();
		StructuralModel modelH0 = new StructuralModel(50, distX, new NormalDistribution(0.0,0.0), distEps, b0,b1);
		for (double s : s2Ksi) {
			StructuralModel modelH1 = new StructuralModel(50, distX, new NormalDistribution(0.0,s), distEps, b0,b1);
			StatTestTester tester = new StatTestTester(500, test, modelH0, modelH1);
//			tester.testEstVarH1();
//			System.out.println(distX.toString() + "," + distEps.toString() + "," + b1 + "," + s + "," + 
//					tester.getMeanDiff() + "," + tester.getVarDiff());
			tester.testAlpha_Beta();	
			System.out.println(distX.toString() + "," + s + "," + tester.getAlpha() + "," + tester.getBeta());
			
		}
//		out.flush();
//		out.close();
			
	}
	
	public static void main (String[] ch) throws FileNotFoundException {
		ErrorsInVariablesTestMain main = new ErrorsInVariablesTestMain();
		main.doWork();
	}
	
}
